package com.gable.report.controller;

import com.gable.report.common.util.ReturnResultUtil;
import com.gable.report.model.ChartsAreaQuery;
import com.gable.report.model.User;
import com.gable.report.service.ChartsForAreaAllocationService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 杨青桦
 * @Description: 区报表资产配置接口页面
 * @Date: 2018/5/29 10:51
 */
@Controller
@RequestMapping("charts/area/allocation")
public class ChartsForAreaAllocationController {

    private final Log logger = LogFactory.getLog(this.getClass());

    @Resource
    HttpServletRequest request;

    @Autowired
    ChartsForAreaAllocationService chartsForAreaAllocationService;

    @RequestMapping("/hospital-page")
    public String pageHospital(){
        User user = (User) request.getSession().getAttribute("loginUser");
        if(user.getGovernmentType() == null){
            return "charts-authority";
        }
        return "charts/area/allocation-hospital";
    }

    @RequestMapping("/type-page")
    public String pageType(){
        User user = (User) request.getSession().getAttribute("loginUser");
        if(user.getGovernmentType() == null){
            return "charts-authority";
        }
        return "charts/area/allocation-type";
    }

    @RequestMapping("/model-page")
    public String pageModel(){
        User user = (User) request.getSession().getAttribute("loginUser");
        if(user.getGovernmentType() == null){
            return "charts-authority";
        }
        return "charts/area/allocation-model";
    }


    /** 
     * @Author: 杨青桦
     * @Description:  主页总览概览框接口
     * @Date: 2017/7/19 16:04
     * @param chartsAreaQuery
     * @return
     */
    @RequestMapping(value="/overview-hospital",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryOverviewHospital(ChartsAreaQuery chartsAreaQuery){
        User user = (User) request.getSession().getAttribute("loginUser");
        Map<String,Object> map;
        try {
            if(user.getGovernmentType() == null){
                return new ReturnResultUtil(0,"该用户不存在");
            }
            map = chartsForAreaAllocationService.queryAllAssetsForHospitalIndex(user, chartsAreaQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }

    /**
     * @Author: 杨青桦
     * @Description:
     * @Date: 2018/5/31 14:49
     * @param chartsAreaQuery
     * @return
     */
    @RequestMapping(value="/overview-type",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryOverviewType(ChartsAreaQuery chartsAreaQuery){
        User user = (User) request.getSession().getAttribute("loginUser");
        Map<String,Object> map;
        try {
            if(user.getGovernmentType() == null){
                return new ReturnResultUtil(0,"该用户不存在");
            }
            map = chartsForAreaAllocationService.queryAllAssetsForDeviceIndex(user, chartsAreaQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }


    /**
     * @Author: 杨青桦
     * @Description:
     * @Date: 2018/5/31 14:49
     * @param chartsAreaQuery
     * @return
     */
    @RequestMapping(value="/overview-model",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryOverviewModel(ChartsAreaQuery chartsAreaQuery){
        User user = (User) request.getSession().getAttribute("loginUser");
        Map<String,Object> map;
        try {
            if(user.getGovernmentType() == null){
                return new ReturnResultUtil(0,"该用户不存在");
            }
            map = chartsForAreaAllocationService.queryAllAssetsForDeviceAgeLimit(user, chartsAreaQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }

    /** 
     * @Author: 杨青桦
     * @Description:  主维度 -- 图表
     * @Date: 2017/7/19 16:30
     * @param chartsAreaQuery
     * @return
     */
    @RequestMapping(value="/hospital",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryHospitals(ChartsAreaQuery chartsAreaQuery){
        User user = (User)request.getSession().getAttribute("loginUser");
        Map<String,Object> map;
        try {
            if(user.getGovernmentType() == null){
                return new ReturnResultUtil(0,"该用户不存在");
            }
            map = chartsForAreaAllocationService.queryHospitals(user, chartsAreaQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }

    /** 
     * @Author: 杨青桦
     * @Description:  设备分类和型号
     * @Date: 2017/7/19 16:30
     * @param chartsAreaQuery
     * @return
     */
    @RequestMapping(value="/type",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryDeviceTypes(ChartsAreaQuery chartsAreaQuery){
        User user = (User)request.getSession().getAttribute("loginUser");
        Map<String,Object> map;
        try {
            if(user.getGovernmentType() == null){
                return new ReturnResultUtil(0,"该用户不存在");
            }
            map = chartsForAreaAllocationService.queryDeviceTypes(user, chartsAreaQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }

    /**
     * @Author: 杨青桦
     * @Description:  设备型号
     * @Date: 2017/7/19 16:30
     * @param chartsAreaQuery
     * @return
     */
    @RequestMapping(value="/model",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryDeviceModels(ChartsAreaQuery chartsAreaQuery){
        User user = (User)request.getSession().getAttribute("loginUser");
        Map<String,Object> map;
        try {
            if(user.getGovernmentType() == null){
                return new ReturnResultUtil(0,"该用户不存在");
            }
            map = chartsForAreaAllocationService.queryDeviceModels(user, chartsAreaQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }


    /**
     * @Author: 杨青桦
     * @Description:  设备厂家
     * @Date: 2018/6/1 14:32
     * @param chartsAreaQuery
     * @return
     */
    @RequestMapping(value="/brand",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryDeviceBrands(ChartsAreaQuery chartsAreaQuery){
        User user = (User)request.getSession().getAttribute("loginUser");
        Map<String,Object> map;
        try {
            if(user.getGovernmentType() == null){
                return new ReturnResultUtil(0,"该用户不存在");
            }
            map = chartsForAreaAllocationService.queryDeviceBrands(user, chartsAreaQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }


    /**
     * @Author: 杨青桦
     * @Description:  查询具体设备详情
     * @Date: 2018/6/1 10:11
     * @param chartsAreaQuery
     * @return
     */
    @RequestMapping(value="/assets",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryAssetsDetail(ChartsAreaQuery chartsAreaQuery){
        User user = (User)request.getSession().getAttribute("loginUser");
        Map<String,Object> map;
        try {
            if(user.getGovernmentType() == null){
                return new ReturnResultUtil(0,"该用户不存在");
            }
            map = chartsForAreaAllocationService.queryDeviceInfoByModelName(user, chartsAreaQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }


    /**
     * @Author: 杨青桦
     * @Description: 年份数据
     * @Date: 2017/7/20 11:42
     * @param chartsAreaQuery
     * @return
     */
    @RequestMapping(value="/year-trend",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryYearTrend(ChartsAreaQuery chartsAreaQuery){
        User user = (User)request.getSession().getAttribute("loginUser");
        Map<String,Object> map = new HashMap<>();
        try {
            if(user.getGovernmentType() == null){
                return new ReturnResultUtil(0,"该用户不存在");
            }
            List<Map<String,Object>> list = chartsForAreaAllocationService.queryYearTrend(user, chartsAreaQuery);
            map.put("list",list);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }


    /**
     * @Author: 杨青桦
     * @Description: 查询新增或者报废设备信息
     * @Date: 2017/12/20 10:47
     * @param chartsAreaQuery
     * @return
     */
    @RequestMapping(value="/new-or-scrap",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryNewlyOrScrapAssetsInfo(ChartsAreaQuery chartsAreaQuery){
        User user = (User)request.getSession().getAttribute("loginUser");
        Map<String,Object> map;
        try {
            if(user.getGovernmentType() == null){
                return new ReturnResultUtil(0,"该用户不存在");
            }
            map = chartsForAreaAllocationService.queryNewlyOrScrapAssetsInfo(user, chartsAreaQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }


}
